﻿using System;
using System.Collections.Generic;
using System.Text;
using ChainAnalises.Classes.IntervalAnalysis;
using ChainAnalises.Classes.Root.SimpleTypes;
using ChainAnalises.Classes.Statistics.MarkovChain.Generators;

namespace ChainAnalises.Classes.PhantomChains
{
    public class TreeTop:ITreeNode
    {
        private PhantomTable Table = null;
        private IGenerator Generator = null;
        private bool IsString = false;

        public TreeTop(BaseChain InputChain, IGenerator Gen)
        {
            Table = new PhantomTable(InputChain);
            Generator = Gen;
            StartPosition = 0;
            volume = Table[0].Volume;
            level = -1;
            if ((Table[1].Content[0] is ValueString) || (Table[1].Content[0] is BaseChain))
            {
                IsString = true;
            }
            if ((InputChain != null) && (InputChain.Length != 0))
            {
                MessagePhantom temp = (MessagePhantom)InputChain[0];
                for (int i = 0; i < temp.power; i++)
                {
                    Children.Add(new TreeNode(this, temp[i], Table));
                }
            }
        }

        public override bool Decrement()
        {
            volume--;
            for (int i = 0; i < Children.Count; i++)
            {
                
                if (Children[i].Volume == 0)
                {
                    Children.RemoveAt(i);
                    return false;
                }
            }
            return true;
        }

        public BaseChain Generate()
        {
            BaseChain Result = null;
            if(IsString)
            {
                Result = new BaseChain(3 * (Table.Length - 1));
            }
            else
            {
                Result = new BaseChain(Table.Length - 1);
            }
            
            this.Find(Result,Generator,Table);
            return Result;
        }
    }
}
